<template>
    <jb-crud-page
        ref="pageRef"
        searchUrl="/base/baseList"
        :conditions-align="'start'"
        :searchConditions="pageConditions"
        @afterSearch="formatData"
        :editComponent="ModelEdit"
    >
        <template #conditions-form>
            <jb-input
                v-model:value="pageConditions.keywords"
                placeholder="请输入关键词搜索"
                style="width: 200px"
            />
            <jb-select v-if="!props.id"
                       :disabled="props.id"
                       v-model:value="pageConditions.coltype"
                       :filterable="true"
                       style="width: 160px"
                       placeholder="请选择企业规模"
                       :options="options">
            </jb-select>
        </template>
        <template #opt>
            <jb-btn type="primary"
                    secondary
                    :modalComponent="ModelEdit"
                    :modalWidth="'70%'">添加企业</jb-btn>
        </template>
        <template #default="{list,tableStartIndex}">
            <jb-table
                :data="list"
                :start-index="tableStartIndex"
            >
                <jb-column type="seq" title="序号" width="60" fixed="left"/>
                <jb-column
                    field="compName"
                    title="企业名称"
                    min-width="160"
                    fixed="left"
                />
                <jb-column
                    field="compScale"
                    title="企业规模"
                    min-width="160"
                    fixed="left"
                >
                </jb-column>
                <jb-column
                           field="compAddr"
                           title="公司地址"
                           min-width="160"
                           fixed="left"
                />
                <jb-column
                    field="compNetwork"
                    title="公司网址"
                    min-width="160"
                />

                <jb-column
                    field="compPhone"
                    title="联系电话"
                    min-width="160"
                />

                <jb-column
                    field="compCode"
                    title="邮政编码"
                    min-width="140"
                    fixed="left"
                >
                </jb-column>
                <jb-column
                    field="compTbAllCnt"
                    title="投标员工总人数"
                    min-width="160"
                />
                <jb-column
                    field="compTbHighCnt"
                    title="投标高级职称人数"
                    min-width="160"
                />
                <jb-column
                    field="compTbMidCnt"
                    title="投标中级职称人数"
                    min-width="160"
                />
                <jb-column
                    field="compTbLowCnt"
                    title="投标初级职称人数"
                    min-width="160"
                />
                <jb-column
                    field="compTbWorkers"
                    title="投标技工人数"
                    min-width="160"
                />


                <jb-column
                    field="compFormDate"
                    title="生产安全许可证有效期"
                    min-width="160"
                />

                <jb-column
                    field="compTbMangerCnt"
                    title="投标项目经理人数"
                    min-width="300"
                />
                <jb-column
                    title="操作"
                    width="120"
                    fixed="right"
                >
                    <template #default="{ row }">
                        <jb-btn class="mx-8px" tip-text="编辑" secondary circle type="warning" :icon="Icons.EDIT"
                                @click="pageRef.showEditModal('编辑企业信息',{id:row.id})"
                        ></jb-btn>
                        <jb-btn
                            type="error"
                            class="mx-8px"
                            :icon="Icons.DELETE"
                            secondary
                            circle
                            :confirmText="'确认删除此文件？'"
                            :url="`/aiFile/delete/${row.id}`"
                            @success="pageRef.loadData()"
                        />
                    </template>
                </jb-column>
            </jb-table>
        </template>
    </jb-crud-page>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {Icons} from '@/constants'
import {useMessage,useNotification} from 'naive-ui'
import {useResetableData} from '@/hooks/common/use-resetable-data'
import {JBoltApi} from "@/service/request/index.js";
import ModelEdit from './model-edit.vue'

const props = defineProps({
    id: String,
})

const message = useMessage()
const notification = useNotification()
const pageRef = ref(null)
const options=[
    {text:'特大型企业', value:'maxType'},{text:'大型', value:'bigType'},{text:'中型', value:'midType'},{text:'小型',value:'smaType'},{text:'微型',value:'micType'},
]

// 页面查询条件
const pageConditions = useResetableData({
    keywords: '',
    coltype: '1',
    knowledgeBaseId: props.id,
    trainType: null,
    status: null,
})

function handleSubmit({componentRef, close}) {
    componentRef.submit(() => {
        close()
        pageRef.value.loadData()
    })
}

// 处理删除
// const handleDelete = async (row) => {
//     try {
//         await pageRef.value.showConfirm('确认要删除该文档吗？')
//         // TODO: 调用删除API
//         message.success('删除成功')
//         pageRef.value.loadData()
//     } catch (error) {
//         console.error(error)
//     }
// }


/** ========ai 生成相关 ============== **/
const models = ref([]);

// const isGenerating = ref(false);
function loadModels() {
    JBoltApi.tryGet('/aiResource/listAllEnabledType').then((res) => {
        models.value = [{label: '选择一个大模型资源', value: '', disabled: true}, ...res.data.map(item => ({label: item.name, value: item.sn}))]
    }).catch(error => {
        console.error(error)
    })
}

function generateByAi(row, model) {
    console.debug(row.id)
    row.isGenerating = true
    console.debug(`使用${model}生成问题`);
    JBoltApi.tryPost('/aiFile/batchAddQuestionByFile', {
        modelSn: model,
        file: row,
    }).then(res => {
        console.debug("生成结果：", res.data)
        row.isGenerating = false;
        notification['success']({
            content: '批量生成问题时间较长,生成的问题的将在[AI向量化文本]中,请耐心等待!',
            duration: 2500,
            keepAliveOnHover: true
        })
    }).catch(e => {
        console.error(e);
        row.isGenerating = false;
    })
}

onMounted(() => {
    loadModels()
    pageRef.value.loadData()
        .then(() => {
            const list = pageRef.value.data;
            if (list) {
                list.forEach(item => {
                    item.isGenerating = false;
                });
            }
        })
        .catch(error => {
            console.error('loadData failed:', error);
        });
})
</script>

<style scoped>
.jb-crud-page :deep(.conditions-form) {
    gap: 12px;
}
</style>
